#include "..\client\iregclient.h"
#include "stdio.h"
#include "windows.h"
#include "stdlib.h"
#include "..\client\errorcode.h"
#include "objbase.h"

typedef IRegClient* (*PFNGetIRegClient)();


int main(int argc, char* argv)
{
    int nRet = Success;
    IRegClient* pIRC = NULL;
    HMODULE  hMod = NULL;
    do 
    {
        hMod = LoadLibrary("client.dll");

        if (!hMod)
        {
            printf("failed to load client.dll(%d)\n",GetLastError());
            break;
        }

        PFNGetIRegClient pfnGetIRegClient = (PFNGetIRegClient)GetProcAddress(hMod,"GetIRegClient");
        if (pfnGetIRegClient == NULL)
        {
            printf("failed to get proc address \n");
            break;
        }

        pIRC = pfnGetIRegClient();
        if (pIRC == NULL)
        {
            printf("error\n");
            break;
        }
        BasicInfomation info;
        info.user = "admin";
        info.pass = "admin888";
        info.hwid = "40291409402914094029140940291409";
        info.pkey = "CD8AF559B55B46119CE47C9338B03E96";
        info.email = "admin@admin.com";

        if (!pIRC->Initialize("localhost",(unsigned short)9977))
        {
            printf("failed to initialize\n");
            break;
        }

        printf("initialize successfully...\n");

        if (!pIRC->Test())
        {
            printf("failed to Test\n");
            break;
        }
        printf("Test successfully!\n");

        int nRet = pIRC->Register(&info);
        if (nRet != Success)
        {
            printf("register failed with error : %s\n", pIRC->ErrorCodeToString(nRet).c_str());
            break;
        }

        printf("register successfully!\n");

        nRet = pIRC->Login(&info);
        if (Success != nRet)
        {
            printf("login failed with error : %s\n", pIRC->ErrorCodeToString(nRet).c_str());
            break;
        }
        printf("login successfully!\n");

        const string new_password = "40291409";
        nRet = pIRC->ChangePassword(new_password);
        if (nRet != Success)
        {
            printf("change password failed with error : %s\n", pIRC->ErrorCodeToString(nRet).c_str());
            break;
        }

        info.pass = new_password;
        nRet = pIRC->UnBind(&info);
        if (nRet != Success)
        {
            printf("unbind failed with error : %s\n", pIRC->ErrorCodeToString(nRet).c_str());
            break;
        }

        printf("unbind successfully!\n");

        info.hwid = "12341234123412341234123412341234";
        info.pass = "40291409";
        nRet = pIRC->Login(&info);
        if (nRet != Success)
        {
            printf("re-login failed\n");
            break;
        }

        printf("re-login successfully...");
    } while (false);

    if (pIRC)
    {
        pIRC->Unintialize();
    }

    if (hMod)
    {
        FreeLibrary(hMod);
    }

    system("pause");
}